const router = require('koa-router')();

// 获取校车列表
router.post('/api/getSchoolBusList', async (ctx, next) => {
  const {
    body: { place, time },
  } = ctx.request;

  const { studentID } = ctx.session || {};

  try {
    const obj = await global._db.model.schoolbus.getSchoolBusList(place, time,studentID);
    ctx.body = {
      code: 0,
      message: 'success',
      data: obj,
    };
  } catch (error) {
    global._logger.error('getSchoolBusList error::', error);
    ctx.body = {
      code: -1,
      message: '获取校车列表失败',
    };
  }
});

// 获取校车地点列表
router.get('/api/getSchoolBusPlaceList', async (ctx, next) => {
  try {
    const obj = await global._db.model.schoolbus.getSchoolBusPlaceList();
    ctx.body = {
      code: 0,
      message: 'success',
      data: obj,
    };
  } catch (error) {
    global._logger.error('getSchoolBusPlaceList error::', error);
    ctx.body = {
      code: -1,
      message: '获取校车筛选列表失败',
    };
  }
});

// 获取校车线路详情
router.post('/api/getSchoolBusDetail', async (ctx, next) => {
  const {
    body: { busNo},
  } = ctx.request;

  const { studentID } = ctx.session || {};

  try {
    const obj = await global._db.model.schoolbus.getSchoolBusDetail(busNo,studentID);
    ctx.body = {
      code: 0,
      message: 'success',
      data: obj,
    };
  } catch (error) {
    global._logger.error('getSchoolBusDetail error::', error);
    ctx.body = {
      code: -1,
      message: '获取校车线路详情失败',
    };
  }
});

// 收藏/取消收藏线路
router.post('/api/favoriteSchoolBus', async (ctx, next) => {
  const {
    body: { busNo},
  } = ctx.request;

  const { studentID } = ctx.session || {};

  try {
    const obj = await global._db.model.schoolbus.favoriteSchoolBus(busNo,studentID);
    ctx.body = {
      code: 0,
      message: 'success',
      data: obj,
    };
  } catch (error) {
    global._logger.error('favoriteSchoolBus error::', error);
    ctx.body = {
      code: -1,
      message: '收藏/取消收藏线路失败',
    };
  }
});

module.exports = router;